package com.app.controller;

import com.app.entity.AppInfo;
import com.app.entity.AppVersion;
import com.app.entity.DataDictionary;
import com.app.service.maintain.MaintainService;
import com.app.utils.Constants;
import com.app.utils.PageSupport;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/user")
public class MaintainController {

    private Logger logger=Logger.getLogger(MaintainController.class);
    private String SoftwareName=null;
    private Integer queryStatus = null;
    private Integer queryCategoryLevel1 = null;
    private Integer queryCategoryLevel2 = null;
    private Integer queryCategoryLevel3 = null;
    private Integer queryPlatformId = null;

    @Autowired
    private MaintainService main;

    @RequestMapping("show_All")
    public String show(Model model,@RequestParam(value = "querySoftwareName",required = false) String querySoftwareName,
                       @RequestParam(value = "queryStatus",required = false) String _queryStatus,
                       @RequestParam(value = "queryCategoryLevel1",required = false) String _queryCategoryLevel1,
                       @RequestParam(value = "queryCategoryLevel2",required = false) String _queryCategoryLevel2,
                       @RequestParam(value = "queryCategoryLevel3",required = false) String _queryCategoryLevel3,
                       @RequestParam(value = "queryPlatformId",required = false) String _queryPlatformId,
                       @RequestParam(value = "pageIndex",required = false) String pageIndex){

        //页面容量
        int pageSize= Constants.pageSize;
        //当前页码
        int currentPageNo=1;
        if (pageIndex!=null){
            currentPageNo=Integer.valueOf(pageIndex);
        }
        //当前条件
        if(querySoftwareName!=null&&!querySoftwareName.equals("")){
            SoftwareName=querySoftwareName;
        }/*else {
            SoftwareName=null;
        }*/
        if(_queryStatus != null&&!_queryStatus.equals("")){
            queryStatus = Integer.parseInt(_queryStatus);
        }/*else {
            queryStatus=null;
        }*/

        if(_queryCategoryLevel1 != null&&!_queryCategoryLevel1.equals("")){
            queryCategoryLevel1 = Integer.parseInt(_queryCategoryLevel1);
        }/*else{
            queryCategoryLevel1=null;
        }*/

        if(_queryCategoryLevel2 != null&&!_queryCategoryLevel2.equals("")){
            queryCategoryLevel2 = Integer.parseInt(_queryCategoryLevel2);
        }/*else {
            queryCategoryLevel2=null;
        }*/

        if(_queryCategoryLevel3 != null&&!_queryCategoryLevel3.equals("")){
            queryCategoryLevel3 = Integer.parseInt(_queryCategoryLevel3);
        }/*else {
            queryCategoryLevel3=null;
        }*/

        if(_queryPlatformId != null&&!_queryPlatformId.equals("")){
            queryPlatformId = Integer.parseInt(_queryPlatformId);
        }/*else {
            queryPlatformId=null;
        }*/
        //总数量
        int totalCount=main.count(SoftwareName,queryStatus,queryCategoryLevel1,queryCategoryLevel2,queryCategoryLevel3,queryPlatformId);

        //总页数
        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount=pages.getTotalPageCount();
        //控制首页和结尾
        if (currentPageNo<1){
            currentPageNo=1;
        }else if (currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        List<AppInfo> list=null;
        try {
            list=main.show_all(SoftwareName,queryStatus,queryCategoryLevel1,queryCategoryLevel2,queryCategoryLevel3,queryPlatformId,currentPageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<DataDictionary> list_App= main.show_type("APP_STATUS");
        List<DataDictionary> list_PT= main.show_type("APP_PLATFORM");
        model.addAttribute("app",list_App);
        model.addAttribute("pt",list_PT);
        model.addAttribute("info",list);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",currentPageNo);
        return "maintain";
    }

    @RequestMapping("show_SH")
    public String show_SH(Model model,@RequestParam(value = "querySoftwareName",required = false) String querySoftwareName,
                          @RequestParam(value = "queryStatus",required = false) String _queryStatus,
                          @RequestParam(value = "queryCategoryLevel1",required = false) String _queryCategoryLevel1,
                          @RequestParam(value = "queryCategoryLevel2",required = false) String _queryCategoryLevel2,
                          @RequestParam(value = "queryCategoryLevel3",required = false) String _queryCategoryLevel3,
                          @RequestParam(value = "queryPlatformId",required = false) String _queryPlatformId,
                          @RequestParam(value = "pageIndex",required = false) String pageIndex){
        //页面容量
        int pageSize= Constants.pageSize;
        //当前页码
        int currentPageNo=1;
        if (pageIndex!=null){
            currentPageNo=Integer.valueOf(pageIndex);
        }
        //当前条件
        if(querySoftwareName!=null&&!querySoftwareName.equals("")){
            SoftwareName=querySoftwareName;
        }
        if(_queryStatus != null && !_queryStatus.equals("")){
            queryStatus = Integer.parseInt(_queryStatus);
        }

        if(_queryCategoryLevel1 != null && !_queryCategoryLevel1.equals("")){
            queryCategoryLevel1 = Integer.parseInt(_queryCategoryLevel1);
        }

        if(_queryCategoryLevel2 != null && !_queryCategoryLevel2.equals("")){
            queryCategoryLevel2 = Integer.parseInt(_queryCategoryLevel2);
        }

        if(_queryCategoryLevel3 != null && !_queryCategoryLevel3.equals("")){
            queryCategoryLevel3 = Integer.parseInt(_queryCategoryLevel3);
        }

        if(_queryPlatformId != null && !_queryPlatformId.equals("")){
            queryPlatformId = Integer.parseInt(_queryPlatformId);
        }
        //总数量
        int totalCount=main.count(SoftwareName,queryStatus,queryCategoryLevel1,queryCategoryLevel2,queryCategoryLevel3,queryPlatformId);
        //总页数
        PageSupport pages=new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount=pages.getTotalPageCount();
        //控制首页和结尾
        if (currentPageNo<1){
            currentPageNo=1;
        }else if (currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        List<AppInfo> list=null;
        try {
            list=main.show_all(SoftwareName,queryStatus,queryCategoryLevel1,queryCategoryLevel2,queryCategoryLevel3,queryPlatformId,currentPageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<DataDictionary> list_App= main.show_type("APP_STATUS");
        List<DataDictionary> list_PT= main.show_type("APP_PLATFORM");
        model.addAttribute("app",list_App);
        model.addAttribute("pt",list_PT);
        model.addAttribute("info",list);
        model.addAttribute("totalPageCount",totalPageCount);
        model.addAttribute("totalCount",totalCount);
        model.addAttribute("currentPageNo",currentPageNo);

        return "mainSH";

    }

    @RequestMapping("/appShow")
    public String appInfoShow(@RequestParam("id") int id,Model model)throws Exception{
        AppInfo appInfo = main.appInfoListShow(id);
        List<AppInfo> appVersion = main.appVersionListShow(id);
        model.addAttribute("appInfo",appInfo);
        model.addAttribute("appVersion",appVersion);
        return "appList";
    }

    @RequestMapping("/appSH")
    public String appInfoSH(@RequestParam("id") int id,Model model)throws Exception{
        AppInfo appInfo = main.appInfoListShow(id);
        AppVersion appVersion = main.appVersionList_SH(id);
        model.addAttribute("appInfo",appInfo);
        model.addAttribute("appVersion",appVersion);
        return "appSH";
    }
}
